package com.example.eduservice.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.commonsutils.R;
import com.example.eduservice.entity.EduComment;
import com.example.eduservice.entity.frontvo.CommentFrontVO;
import com.example.eduservice.service.EduCommentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author:tjf
 * @Description:
 */
@Tag(name = "前端课程评论控制器")
@RestController
@RequestMapping("/eduservice/course/comment")
public class CommentFrontController {
    @Resource
    private EduCommentService commentService;
    @Operation(description = "保存课程评论")
    @PostMapping("/")
    public R saveCourseComment(@RequestBody CommentFrontVO commentVO, @RequestHeader(name = "token", required = false)String token){
        if (StringUtils.isNotEmpty(token)){
            commentService.saveCourseComment(commentVO, token);
            return R.ok();
        }
        return R.error().message("token无效，请登录！");
    }
    @Operation(description = "根据课程id和教师id，获取评论并分页")
    @GetMapping("/{courseId}/{teacherId}/{current}/{size}")
    public R queryCourseComment(@PathVariable("courseId") String courseId,
                                @PathVariable("teacherId") String teacherId,
                                @PathVariable("current") Integer current,
                                @PathVariable("size") Integer size){
        Map<String, Object> comments = commentService.queryCourseComment(courseId, teacherId, new Page<EduComment>(current, size));
        return R.ok().data("comments", comments);
    }
    @GetMapping("/replies/{replyCommentId}/{current}/{size}")
    public R queryCommentRepliesByCommentId(@PathVariable("replyCommentId") String replyCommentId,
                                            @PathVariable("current") Integer current,
                                            @PathVariable("size") Integer size){
        Map<String, Object> map = commentService.queryCommentReplies(replyCommentId,
                                                        new Page<EduComment>(current, size));

        return R.ok().data("replies", map);
    }
}
